<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\api\controller;

use Think\Db;

/**
 * 轮播
 */
class Booklist extends Common
{
    /**
     * [__construct 构造方法]
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();
    }

    /**
     * [Index 入口]
     * 书单列表
     *
     */
    public function Index()
    {
            // 参数
        $params = $this->data_request; 

     
        // 分页
        $number = isset($params['number'])? intval($params['number']):10;
        $page = max(1, isset($params['page']) ? intval($params['page']) : 1);

          // 获取总数
        $total = Db::name('books_list')->count();
        $page_total = ceil($total/$number);
        $start = intval(($page-1)*$number);


        $data=Db::name("books_list")->limit($start,$number)->select();


      foreach ($data as $k => $v) {
        $data[$k]['add_time']=date("Y-m-d H:i:s",$v['add_time']);
        $data[$k]['upd_time']=date("Y-m-d H:i:s",$v['upd_time']);
      }

          // 返回数据
        $result = [
            'total'         =>  $total,
            'page_total'    =>  $page_total,
            'data'          =>  $data,
        ];

        return DataReturn('success', 0, $result);
    }

    /*
     *推荐书单
     */
    public function Recommendedlist(){
          // 参数
        $params = $this->data_request; 

     
        // 分页
        $number = isset($params['number'])? intval($params['number']):10;
        $page = max(1, isset($params['page']) ? intval($params['page']) : 1);

          // 获取总数
        $total = Db::name('books_list')->where(['is_recommended'=>1])->count();
        $page_total = ceil($total/$number);
        $start = intval(($page-1)*$number);


        $data=Db::name("books_list")->where(['is_recommended'=>1])->limit($start,$number)->select();


    	foreach ($data as $k => $v) {
    		$data[$k]['add_time']=date("Y-m-d H:i:s",$v['add_time']);
    		$data[$k]['upd_time']=date("Y-m-d H:i:s",$v['upd_time']);
    	}

          // 返回数据
        $result = [
            'total'         =>  $total,
            'page_total'    =>  $page_total,
            'data'          =>  $data,
        ];

        return DataReturn('success', 0, $result);

    }
    
    
     /*
    *书单内容
     */
    public function book_good_list(){
          // 参数
        $params = $this->data_request; 

        $goods_id=Db::name("books_goods")->where(['books_id'=>$params['book_id']])->column("goods_id");

        $goods=Db::name("goods")->where('id',"in",$goods_id)->field("id,inventory,title,price,original_price,images,author")->select();

        foreach ($goods as $key => $value) {
          if($value['author']){
            $goods[$key]['author_name']=Db::name("author")->where(['id'=>$value['author']])->value("name");
          }else{
            $goods[$key]['author_name']="未知";
          }
        }

       return  DataReturn('success', 0, $goods);

    }
}
?>